from django.shortcuts import render, redirect
from app01 import models
# 使用modelform
from app01.utils.forms import UserEditModelForm, UserAddModelForm
from app01.utils.pagination import Pagination
from app01.utils.forms import ManagerAddModelForm


# Create your views here.
def manager_user_list(request):
    """管理员可见的用户列表"""

    # 批量创建用于测试
    # num = 111112
    # for i in range(1000):
    #     n = str(num)
    #     num = num + 1
    #     print(num)
    #     models.User.objects.create(User_name=n, User_email='1@1.com', User_account=1, User_company='1',
    #                                User_nicheng='1', User_password='1111111111a')

    # 检查是否登录

    data_dict = {}
    search_data = request.GET.get('query', "")
    if search_data:
        data_dict['User_name__contains'] = search_data
    from app01.utils.pagination import Pagination
    queryset = models.User.objects.filter(**data_dict)
    page_object = Pagination(request, queryset)
    context = {
        'queryset': page_object.page_queryset,
        'search_data': search_data,
        'page_string': page_object.createHtml(),
    }
    return render(request, "manager_user_list.html", context)


def manager_user_add(request):
    """管理员添加用户"""
    title = '新建用户'
    if request.method == "GET":
        form = UserAddModelForm()
        return render(request, 'manager_AddorRegister.html', {"form": form, 'title': title})
    # 用post提交,数据校验
    form = UserAddModelForm(data=request.POST)
    if form.is_valid():
        # print(form.cleaned_data)
        form.save()
        return redirect('/manager/user/list/')
    content = {
        "form": form,
        'title': '新建用户',
    }
    return render(request, 'manager_AddorRegister.html', content)


def manager_user_delete(request, nid):
    """管理员删除用户"""
    models.User.objects.filter(id=nid).delete()
    return redirect('/manager/user/list/')


def manager_user_edit(request, nid):
    """管理员界面的用户信息修改"""
    title = '修改用户'
    row_object = models.User.objects.filter(id=nid).first()
    if request.method == "GET":
        form = UserEditModelForm(instance=row_object)
        return render(request, "manager_AddorRegister.html", {'form': form, 'title': title})
    models.User.objects.filter(id=nid).update()

    form = UserEditModelForm(data=request.POST, instance=row_object)
    if form.is_valid():
        # print(form.cleaned_data)
        form.save()
        return redirect('/manager/user/list/')
    return render(request, 'manager_AddorRegister.html', {"form": form, 'title': title})


def manager_manager_list(request):
    """管理员列表"""
    # 批量创建用于测试
    # num = 111112
    # for i in range(1000):
    #     n = str(num)
    #     num = num + 1
    #     print(num)
    #     models.Manager.objects.create(
    #         Manager_name=n,
    #         Manager_password='111111111z',
    #     )
    data_dict = {}
    search_data = request.GET.get('query', "")
    print(search_data)
    if search_data:
        data_dict["Manager_name__contains"] = search_data
    queryset = models.Manager.objects.filter(**data_dict)

    page_object = Pagination(request, queryset)
    context = {
        'queryset': page_object.page_queryset,
        'page_string': page_object.createHtml(),
        'search_data': search_data,
    }
    return render(request, "manager_manager_list.html", context)


def manager_manager_add(requset):
    """添加管理员"""
    title = '新建管理员'
    if requset.method == "GET":
        form = ManagerAddModelForm()
        content = {
            'title': title,
            'form': form,
        }
        return render(requset, 'manager_AddorRegister.html', content)
    form = ManagerAddModelForm(data=requset.POST)
    if form.is_valid():
        form.save()
        return redirect('/manager/manager/list/')
    content = {
        'title': title,
        'form': form,
    }
    return render(requset, 'manager_AddorRegister.html', content)


def manager_manager_edit(requset, nid):
    """编辑管理员"""
    row_object = models.Manager.objects.filter(id=nid).first()
    title = "编辑管理员"
    if not row_object:
        # content = {
        #     'form': form,
        #     'title': title,
        #     'msg': '数据错误',
        # }
        # return render(requset, 'error.html', content)

        return redirect("/manager/manager/list/")

    if requset.method == "GET":
        form = ManagerAddModelForm(instance=row_object)
        content = {
            'form': form,
            'title': title,
        }
        return render(requset, 'manager_AddorRegister.html', content)

    form = ManagerAddModelForm(data=requset.POST, instance=row_object)
    print(form.is_valid())
    if form.is_valid():
        form.save()
        return redirect("/manager/manager/list/")
    content = {
        'form': form,
        'title': title,
    }
    return render(requset, 'manager_AddorRegister.html', content)


def manager_manager_delete(requset, nid):
    """删除管理员"""
    models.Manager.objects.filter(id=nid).delete()
    return redirect('/manager/manager/list/')
